﻿//------------------------------------------------------------------------------
// <copyright file="Player.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System.Linq;
using Microsoft.Internal.GamesTest.Rpc.Server;

namespace Rpc.CSharpSample
{
    /// <summary>
    /// Player class used to test RPC server.
    /// </summary>
    public class Player
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Player">Player</see> class.
        /// </summary>
        public Player()
        {
            this.Health = -1.0f;
        }

        /// <summary>
        /// Gets or sets the amount of health the player has.
        /// </summary>
        public float Health
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the player name.
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the spells available to the player.
        /// </summary>
        public string[] Spells
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the weapons available to the player.
        /// </summary>
        public Weapon[] Weapons
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the player bounding box.
        /// </summary>
        public Stature Stature
        {
            get;
            set;
        }

        /// <summary>
        /// Deserialize the Player object from an RPC Archive.
        /// </summary>
        /// <param name="archive">Archive to load from.</param>
        /// <returns>A player object constructed from data in an archive.</returns>
        public static Player FromRpcArchive(RpcArchive archive)
        {
            Player player = new Player();

            player.Health = archive.GetNamedFloatValue("health");
            player.Name = archive.GetNamedStringValue("name");
            player.Spells = archive.GetNamedStringCollection("spells");
            player.Stature = Stature.FromRpcArchive(archive.GetNamedArchiveValue("stature"));
            player.Weapons = (from w in archive.GetNamedArchiveCollection("weapons") select Weapon.FromRpcArchive(w)).ToArray();

            return player;
        }

        /// <summary>
        /// Serialize the Player object to an RPC Archive.
        /// </summary>
        /// <param name="output">Output archive that receives player data.</param>
        public void ToRpcArchive(RpcArchive output)
        {
            output.SetNamedFloatValue("health", this.Health);
            output.SetNamedStringValue("name", this.Name);
            output.SetNamedStringCollection("spells", this.Spells);
            output.SetNamedArchiveValue("stature", this.Stature.ToRpcArchive());
            output.SetNamedArchiveCollection("weapons", (from w in this.Weapons select w.ToRpcArchive()).ToArray());
        }
    }
}
